<?php
/*
 * Session variables:
 * $_SESSION['database'] [string] = database name of the current tutorial centre
 * $_SESSION['type'] [string] = user type of the logged in user
 * $_SESSION['authorised'] [boolean] = whether or not the current user is logged in
 * $_SESSION['user'] [string] = the username/ email of the logged in user
 */

session_start();

if(isset($_SESSION['authorised']) && $_SESSION['authorised'] == true)
{
	if(isset($_SESSION['type']))
	{
		if($_SESSION['type'] == 'administrator')
		{
			header('Location: admin_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'student')
		{
			$host = 'localhost';
			$dbuser = 'postgres';
			$dbpass = 'password';
			$dbname = $_SESSION['database'];
				
			pg_connect("host=$host user=$dbuser password=$dbpass dbname=$dbname") or die(pg_last_error());
				
			$sql = "SELECT * FROM announcements;";
			$result = pg_query($sql);
				
				
				
			?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<style>
.nav {
	width: 210px;
	height: auto;
	border: thin solid #09C;
	margin-left: 10px;
	text-align: center;
}

.mainline {
	width: 712px;
	height: auto;
	text-align: center;
}
</style>
</head>
<body bgcolor="#FFFFFF">
	<center>
		<div id="wrapper">
			<div id="header">
				<h1></h1>
			</div>

			<div id="header2">
				<h3></h3>
			</div>

			<div id="nav">
				<?php include('student_navigation.php');?>
			</div>

			<div id="mainline">
				<h4>Welcome Student</h4>
				<div id="body">
					<h1>Tutorial News</h1>
					<div align="justify">
					<table width="675" cellspacing="3">
						<?php 
						include ('connection.inc');
						$sql = "SELECT * from announcements order by announcement_date desc";
			            $result = pg_query($sql);
						for ($i = 0; $i < pg_num_rows($result); $i++) {
							$title = pg_fetch_result($result, $i, 'announcement_title');
							$timestamp = pg_fetch_result($result, $i, 'announcement_date');
		            		$time = date_create_from_format('Y-m-d G:i:s.u', $timestamp);
		            		$date= date_format($time, 'M-d-Y \a\t g:i a');
							$announcement= pg_fetch_result($result, $i, 'announcement');
							if($i%2==0){
								echo '<tr bgcolor= "e8e8e8"><td>'.
									"<h3>$title</h3><hr>
	        						<h5>$date</h5>
									$announcement <br><br></td></tr>";
							}else{
								echo '<tr bgcolor= "f5f5f5"><td>'.
									"<h3>$title</h3><hr>
	                				<h5>$date</h5>
									$announcement <br><br></td></tr>";
							}
						}
						?>
					</table>
					</div>
				</div>
				<p align="justify">
					<br />
				</p>
			</div>
			<br class="clearfloat"><br />
			<div id="footer">
				<p align="center">
					Tutorial Business Manager: info@tbms.com <br />Credits/Engineers:
					Aquino, Chloebelle; Dela Cruz, Roy Erwin; Maliwanag, Ranyel Bryan;
					Manzano, Audrey Ann; Ng, Sheila Regine<br /> <br />copyright 2011
				</p>
			</div>
		</div>
	</center>
</body>
</html>
<?php
		}
		elseif($_SESSION['type'] == 'tutor')
		{
			header('Location: tutor_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'parent')
		{
			header('Location: parent_index.php');
			exit();
		}
	}
}
else
{
	header('Location: index.php');
}
?>
